import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/services.dart';

import 'http.dart';

class HttpService {
  static const String BASE_URL = 'https://api.douguo.net/';
  static const String GET_BASE_URL = 'https://m.douguo.com/';

  /// 首页推荐
  static Future getHomeRecommendData({int page = 1, int pageSize = 10}) async {
    final response =
        await Http.post('${BASE_URL}home/recommended/${page * pageSize}/10',
            params: {
              '_session': '1628128796-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'auto_play_mode': '2',
              'code': '79c0d8972caf4383',
              'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'direction': page,
              'elapsed_milliseconds': '9223372036854775807',
              'bottom_id': '0',
              'request_count': '10',
              'user_id': '0'
            },
            cancelToken: CancelToken(),
            options: Options());

    /// 字符串转化为Json
    Map<String, dynamic> responseData = jsonDecode(response);
    // print('home/recommended/${page*pageSize}/10');
    // print(responseData);
    return responseData;
  }

  /// 首页关注
  static Future getHomeAttentonData({int page = 1, int pageSize = 10}) async {
    final response =
        await Http.post('${BASE_URL}home/ffeeds/${page * pageSize}/10',
            params: {
              '_session': '1628128796-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'auto_play_mode': '2',
              'bottom_id': '0',
              'code': '79c0d8972caf4383',
              'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'request_count': '2',
              'user_id': '0'
            },
            cancelToken: CancelToken(),
            options: Options());
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 首页笔记
  static Future getHomeNoteData({int page = 1, int pageSize = 20}) async {
    final response =
        await Http.post('${BASE_URL}home/notes/${page * pageSize}/20',
            params: {
              '_session': '1628128796-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'btmid': [30593237, 30593139],
              'code': '79c0d8972caf4383',
              'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'direction': '1',
              'is_new_user': '0',
              'request_count': '2',
              'user_id': '0',
            },
            cancelToken: CancelToken(),
            options: Options());
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 笔记列表
  static Future getNoteListData({int page = 0, int pageSize = 20,id = 3132171}) async {
    final response =
    await Http.postForm('${BASE_URL}note/learned/${page * pageSize}/20',
        params: {
          '_session': '1628128796-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          '_vs': '314',
          'code': '3caaf33cccc26a7b',
          'device_id': '13CB3E69-C216-4B9B-A995-1946AB7B9699',
          'id': id,
          'user_id':'0'
        },
        cancelToken: CancelToken(),
        options: Options());
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 首页视频
  static Future getHomeVideoData({int page = 1, int pageSize = 20}) async {
    final response =
        await Http.post('${BASE_URL}home/videos/${page * pageSize}/20',
            params: {
              '_session': '1628128796-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'auto_play_mode': '2',
              'code': '79c0d8972caf4383',
              'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'request_count': '2',
              'user_id': '0',
            },
            cancelToken: CancelToken(),
            options: Options());
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 首页母婴知识
  static Future getMotherData({int cate = 1, int stage = 1}) async {
    final response = await Http.get(
        '${GET_BASE_URL}baby/lists?cate=${cate}&stage=${stage}&num=10&offset=0',
        params: {},
        cancelToken: CancelToken(),
        options: Options());
    Map<String, dynamic> responseData = jsonDecode(response);
    Map<String, dynamic> info = responseData['data'];
    return info;
  }

  /// 首页 安培烘烤
  static Future getCookFoodData({int offset = 10}) async {
    final response = await Http.get(
        '${GET_BASE_URL}u/u11932230350636/myrecipe?offset=${offset}',
        params: {},
        cancelToken: CancelToken(),
        options: Options());
    Map<String, dynamic> responseData = jsonDecode(response);
    Map<String, dynamic> info = responseData;
    return info;
  }

  /// 首页 活动 https://m.douguo.com/huodongnew/getlists?offset=0&limit=10&laid=0
  static Future getActivityData({int page = 0, int laid = 0}) async {
    final response = await Http.get(
        '${GET_BASE_URL}huodongnew/getlists?offset=${page * 20}&limit=20&laid=${laid}',
        params: {},
        cancelToken: CancelToken(),
        options: Options());
    Map<String, dynamic> responseData = jsonDecode(response);
    Map<String, dynamic> info = responseData['data'];
    return info;
  }

  /// 热门活动列表
  static Future getHottopicData() async {
    final response = await Http.post('${BASE_URL}note/topiclist/0/20',
        params: {
          '_session': '1628128796-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          'code': '79c0d8972caf4383',
          'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          'user_id': '0',
          'type': '0',
        },
        cancelToken: CancelToken(),
        options: Options());
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 热门活动详情
  static Future getHotDetailData(
      {int page = 1, int pageSize = 10, int id = 1044}) async {
    final response =
        await Http.postForm('${BASE_URL}note/topicdetail/${page * pageSize}/10',
            params: {
              '_session': '1628145326-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              '_vs': '1044',
              'code': 'a574665ae8b9a91e',
              'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'id': id,
              'sort': '1',
              'user_id': '0',
            },
            cancelToken: CancelToken(),
            options: Options());
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 笔记详情 -- 相关笔记
  static Future getNotesDetailData(
      {int page = 1, int pageSize = 10, int id = 30530839}) async {
    final response =
        await Http.postForm('${BASE_URL}note/related/${page * pageSize}/10',
            params: {
              '_as':
                  '_session=MTYyOTE4OTAwNy00OTI2RDVEMy0xOERCLTQ4RDctQjg1QS1EMDY1MzUwQzNCQTU=&agent_id=ZjZiYjQxNTlmNmU3N2U4OWY2NWFiYTk4OTkwZTU5Mzg=&code=MDI4ZmIzNTkzM2I3ZDgzMA==&device_id=NDkyNkQ1RDMtMThEQi00OEQ3LUI4NUEtRDA2NTM1MEMzQkE1&id=MzA1MzA4Mzk=&signtime=MTYyOTE4OTA1NA==&user_id=MjY4NDcyMzk=',
              '_session': '1628145326-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'agent_id': 'f6bb4159f6e77e89f65aba98990e5938',
              'code': 'a574665ae8b9a91e',
              'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'id': id,
              'signature': '2b5e94377a68e6f7bd293f64bef94d15',
            },
            cancelToken: CancelToken(),
            options: Options());
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 笔记详情 -- 笔记内容
  static Future getNotesContentData({int id = 30530839}) async {
    final response = await Http.postForm('${BASE_URL}note/detail',
        params: {
          '_as':
              '_session=MTYyOTE4OTAwNy00OTI2RDVEMy0xOERCLTQ4RDctQjg1QS1EMDY1MzUwQzNCQTU=&agent_id=ZjZiYjQxNTlmNmU3N2U4OWY2NWFiYTk4OTkwZTU5Mzg=&code=MDI4ZmIzNTkzM2I3ZDgzMA==&device_id=NDkyNkQ1RDMtMThEQi00OEQ3LUI4NUEtRDA2NTM1MEMzQkE1&id=MzA1MzA4Mzk=&signtime=MTYyOTE4OTA1NA==&user_id=MjY4NDcyMzk=',
          '_session': '1628145326-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          '_vs': '12200',
          'agent_id': 'f6bb4159f6e77e89f65aba98990e5938',
          'code': 'a574665ae8b9a91e',
          'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          'id': id,
          'is_new_user': '0',
          'recall': 'N_HOT_SEARCH',
          'signature': '2b5e94377a68e6f7bd293f64bef94d15',
          'signtime': '1629189054',
          'user_id': '26847239',
        },
        cancelToken: CancelToken(),
        options: Options());
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 做饭详情 -- 做饭内容
  /// https://api.douguo.net/recipe/v2/detail/3112121
  static Future getCookDetailMianData({int id = 3112121}) async {
    final response = await Http.postForm('${BASE_URL}recipe/v2/detail/${id}',
        params: {
          '_analytics': '',
          '_as':
              '_session=MTYyOTE4OTAwNy00OTI2RDVEMy0xOERCLTQ4RDctQjg1QS1EMDY1MzUwQzNCQTU=&agent_id=ZjZiYjQxNTlmNmU3N2U4OWY2NWFiYTk4OTkwZTU5Mzg=&code=MDI4ZmIzNTkzM2I3ZDgzMA==&device_id=NDkyNkQ1RDMtMThEQi00OEQ3LUI4NUEtRDA2NTM1MEMzQkE1&id=MzA1MzA4Mzk=&signtime=MTYyOTE4OTA1NA==&user_id=MjY4NDcyMzk=',
          '_ext': '',
          '_session': '1628145326-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          '_vs': '12200',
          'agent_id': 'f6bb4159f6e77e89f65aba98990e5938',
          'code': 'a574665ae8b9a91e',
          'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          'is_new_user': '1',
          'recall': 'C_HIGHT_LEVEL',
          'signature': '2b5e94377a68e6f7bd293f64bef94d15',
          'signtime': '1629189054',
          'user_id': '26847239',
        },
        cancelToken: CancelToken(),
        options: Options());
    Map<String, dynamic> responseData = jsonDecode(response);
    Map<String, dynamic> result = responseData['result'];
    return result;
  }

  /// 详情 评论 和 大家都在做
  /// https://api.douguo.net/recipe/relatedinfo/3051319
  static Future getDetailCommentsData({int id = 3051319}) async {
    final response = await Http.postForm('${BASE_URL}recipe/relatedinfo/${id}',
        params: {
          '_as':
              '_session=MTYyOTE4OTAwNy00OTI2RDVEMy0xOERCLTQ4RDctQjg1QS1EMDY1MzUwQzNCQTU=&agent_id=ZjZiYjQxNTlmNmU3N2U4OWY2NWFiYTk4OTkwZTU5Mzg=&code=MDI4ZmIzNTkzM2I3ZDgzMA==&device_id=NDkyNkQ1RDMtMThEQi00OEQ3LUI4NUEtRDA2NTM1MEMzQkE1&id=MzA1MzA4Mzk=&signtime=MTYyOTE4OTA1NA==&user_id=MjY4NDcyMzk=',
          '_session': '1628145326-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          'agent_id': 'f6bb4159f6e77e89f65aba98990e5938',
          'code': 'a574665ae8b9a91e',
          'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
          'signature': '2b5e94377a68e6f7bd293f64bef94d15',
          'signtime': '1629189054',
          'user_id': '26847239',
        },
        cancelToken: CancelToken(),
        options: Options());
    Map<String, dynamic> responseData = jsonDecode(response);
    Map<String, dynamic> result = responseData['result'];
    return result;
  }

  /// 作者 -- 做饭列表
  /// https://api.douguo.net/recipe/urecipes/16747718/0/20/0
  static Future getAuthorCookListData({int id = 16747718}) async {
    final response =
        await Http.postForm('${BASE_URL}recipe/urecipes/${id}/0/20/0',
            params: {
              '_as':
                  '_session=MTYzMDA1NDI4OC00OTI2RDVEMy0xOERCLTQ4RDctQjg1QS1EMDY1MzUwQzNCQTU=&agent_id=ZjZiYjQxNTlmNmU3N2U4OWY2NWFiYTk4OTkwZTU5Mzg=&code=ZmYwZDI3YmYxMTJkZTM1Yw==&device_id=NDkyNkQ1RDMtMThEQi00OEQ3LUI4NUEtRDA2NTM1MEMzQkE1&signtime=MTYzMDA1NDI5Nw==&user_id=MjY4NDcyMzk=',
              '_session': '1630054288-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'agent_id': 'f6bb4159f6e77e89f65aba98990e5938',
              'auto_play_mode': '2',
              'code': 'ff0d27bf112de35c',
              'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'signature': 'f590231e608a6dffcb5f71dfbd226c2a',
              'signtime': '1630054297',
              'user_id': '26847239',
            },
            cancelToken: CancelToken(),
            options: Options());
    // Map<String, dynamic> responseData = jsonDecode(response);
    Map<String, dynamic> result = response['result'];
    return result;
  }

  /// 做饭 -- 收藏
  /// https://api.douguo.net/user/ufavorites_v2/0/10
  static Future getAuthorCollectionData({int id = 16747718}) async {
    final response =
        await Http.postForm('${BASE_URL}user/ufavorites_v2/0/10',
            params: {
              '_as':
                  '_session=MTYzMDA1NDI4OC00OTI2RDVEMy0xOERCLTQ4RDctQjg1QS1EMDY1MzUwQzNCQTU=&agent_id=ZjZiYjQxNTlmNmU3N2U4OWY2NWFiYTk4OTkwZTU5Mzg=&code=ZmYwZDI3YmYxMTJkZTM1Yw==&device_id=NDkyNkQ1RDMtMThEQi00OEQ3LUI4NUEtRDA2NTM1MEMzQkE1&signtime=MTYzMDA1NDI5Nw==&user_id=MjY4NDcyMzk=',
              '_session': '1630054288-4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'agent_id': 'f6bb4159f6e77e89f65aba98990e5938',
              'code': 'ff0d27bf112de35c',
              "collect_id": '0',
              'device_id': '4926D5D3-18DB-48D7-B85A-D065350C3BA5',
              'signature': 'f590231e608a6dffcb5f71dfbd226c2a',
              'signtime': '1630054297',
              'user_id': '26847239',
              "visit_user_id": id,
            },
            cancelToken: CancelToken(),
            options: Options());
    Map<String, dynamic> responseData = jsonDecode(response);
    Map<String, dynamic> result = responseData['result'];
    return result;
  }


  /// 获取本地mock数据
  static Future getMockDataWithPath({required String path}) async {
    return HttpService.mock(path: path);
  }

  static Future<dynamic> mock({required String path}) async {
    var responseStr = await rootBundle.loadString(path);
    var responseJson = json.decode(responseStr);
    return responseJson;
  }

}
